<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\Builder;

use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Sort
 *
 * @property int $id
 * @property string $name
 * @property int $parent_id
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property \Carbon\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Post[] $posts
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tag[] $tags
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort name($name)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Sort onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Sort whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Sort withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Sort withoutTrashed()
 * @mixin \Eloquent
 */
class Sort extends Model
{
	use SoftDeletes;
    // protected $table = "sorts";
    protected $guarded = [];
    public $timestamps = true;
    protected $dates = ['deleted_at'];

    /**
     * 重写boot方法
     */
    protected static function boot() {
    	parent::boot();
    	/*static::addGlobalScope('name', function(Builder $builder){
    		$builder->where('name', 'ggg');
    	});*/
    }

    /**
     * 定义一个本地查询作用域
     */
    public function scopeName($query, $name) {
    	$query->where('name', $name);
    }

    /**
     * 定义post和sort的关联关系
     */
    public function posts() {
    	return $this->hasMany('App\Models\Post');
    }

    public function tags() {
        return $this->morphToMany('App\Models\Tag', 'taggable');
    }
}
